import logging
import asyncio
import time
import kopf

# import all controllers
from controller.webservices import controller  # noqa: F401

import urllib3

from common import config
from utils.apis import get_k8s_cluster_domain

# Disable SSL warnings for self-signed certificates
urllib3.disable_warnings()


def bootstrap():
    kopf.configure(
        verbose=config.DEBUG,
    )

    # 设置基础日志级别
    log_level = logging.DEBUG if config.DEBUG else logging.WARNING
    
    logging.basicConfig(
        level=log_level,
        format='%(asctime)s - [%(levelname)s] [%(name)s] %(message)s',
        datefmt="%Y-%m-%dT%H:%M:%S",
    )

    get_k8s_cluster_domain(logging.Logger(__file__))

    loop = asyncio.get_event_loop()
    loop.run_until_complete(
        kopf.operator(
            clusterwide=False,
            priority=int(time.time() * 1000000),
            namespaces=["devops-system"],
        )
    )

    return 0


if __name__ == "__main__":
    try:
        bootstrap()
    except KeyboardInterrupt:
        print("Reconciler stopped by user.")
    except Exception as e:
        print(f"Reconciler failed with error: {e}")
        exit(1)
